package model

import "time"

type RawMaterial struct {
	Id int64  `xorm:"pk autoincr"`
	Name string
	CurrentStock int64
	NegativeInventory bool
	SafetyStock int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type ShoppingCartRawMaterial struct {
	RawMaterial *RawMaterial
	Number int64
}

type ShoppingCart struct {
	ShoppingCartRawMaterialList []*ShoppingCartRawMaterial
}

func Purchasing(sc *ShoppingCart)(map[string]interface{}, error){

	for _, srm := range sc.ShoppingCartRawMaterialList{
		var rm RawMaterial
		x.Id(srm.RawMaterial.Id).Get(&rm)

		if srm.Number > rm.CurrentStock {
			if !rm.NegativeInventory{
				return map[string]interface{}{"message":"此原料不能负库存下单", "data":nil, "code":400}, nil
			}
		}

		rm.CurrentStock -= srm.Number
		x.Id(rm.Id).Update(&rm)
	}

	return map[string]interface{}{"message":"成功", "data":nil, "code":200}, nil
}